package model

import (
	"context"
	"service/ctrl"
	"service/model/mod"

	gpb "gitee.com/cloudapex/protos/hr_cache/pb"

	"google.golang.org/grpc"
	"google.golang.org/grpc/status"
)

var RolesCache rolesCache

func init() {
	ctrl.GCon.RegistClienter(mod.GCONN_HR_CACHE, func(c *grpc.ClientConn) { RolesCache.cli = gpb.NewRolesCacheClient(c) })
}

type rolesCache struct {
	cli gpb.RolesCacheClient
}

// rolesCache.RoleCacheList 获取角色列表
func (this *rolesCache) RoleCacheList(ctx context.Context, in *gpb.RolesBaseCacheReq, roleId int64) (bool, error) {
	var fage bool
	rsp, err := this.cli.GetRoleList(ctx, in)
	if s := status.Convert(err); s.Err() != nil {
		return false, s.Err()
	}

	if len(rsp.RoleMap) > 0 {
		for _, v := range rsp.RoleMap {
			if v.Id == roleId {
				fage = true
			}
		}
	}
	return fage, err
}

// rolesCache.RoleCacheList 添加角色
func (this *rolesCache) AddRole(ctx context.Context, in *gpb.RolesBaseCacheReq) (*gpb.RolesBaseCacheRes, error) {
	return this.cli.AddRole(ctx, in)
}
